package com.jeesite.modules.isys.common.entity;

import com.fasterxml.jackson.annotation.JsonIgnore;
import lombok.Data;

import java.util.HashMap;
import java.util.Map;

@Data
public class Res {


    @JsonIgnore
    private Map  map;

    //消息编码
    private Integer code;
    //消息体
    private  Object data;

    //消息
    private String msg;



    //消息常用编码
    public  static  Integer OK = 200;
    public  static  Integer ERROR = 500;
    public  static  Integer FAIL = 204;
    public  static  Integer NOTFOUND = 404;

    public  static Res ok(String msg){
        return new Res(OK,"",msg);
    }

    public  static Res ok(Object data){
        return new Res(OK,data,"");
    }


    public  static Res ok(Object data,String msg){
        return new Res(OK,data,msg);
    }

    public static Res error() {
        return new Res(ERROR,"","错误");
    }
    public static Res error(String msg) {
        return new Res(ERROR,"",msg);
    }

    public static Res exception(String e) {
        return new Res(ERROR,e,"系统异常，请联系管理员处理");
    }

    public static Res fail() {
        return new Res(FAIL,"","失败");
    }




    //构造的map
    @JsonIgnore
    public  Map<Object,Object> resMap = new HashMap<>();


    public static Res init(Integer code,String  msg){
        return  new Res(code, msg);
    }

    //构造map返回
    public  Res add(Object key,Object value){
        resMap.put(key, value);
        return this;
    }

    public Res build(){
        data = resMap;
        return  this;
    }


    public Res(Integer code, Object data, String msg) {
        this.code = code;
        this.data = data;
        this.msg = msg;
    }

    public Res(Object data, String msg) {
        this.data = data;
        this.msg = msg;
    }
}
